<?php
//=======================================
//###################################
// Kayako Web Solutions
//
// Source Copyright 2001-2004 Kayako Infotech Ltd.
// Unauthorized reproduction is not allowed
// License Number: $%LICENSE%$
// $Author: vshoor $ ($Date: 2006/10/27 11:44:17 $)
// $RCSfile: functions_clientsearch.php,v $ : $Revision: 1.10 $ 
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
//###################################
//=======================================

/**
* Searches the knowledgebase
*/
function searchKnowledgebase($_query)
{
	global $dbCore, $_SWIFT;

	$_kbarticleidlist = $_finalkbarticleidlist = $_queryresult = array();
	$dbCore->query("SELECT `kbarticleid` FROM `". TABLE_PREFIX ."kbarticles` WHERE ". buildSQLSearch("subject", $_query, true)." LIMIT 50;");
	while ($dbCore->nextRecord())
	{
		$_kbarticleidlist[] = $dbCore->Record["kbarticleid"];
	}

	$dbCore->query("SELECT `kbarticleid` FROM `". TABLE_PREFIX ."kbarticledata` WHERE ". buildSQLSearch("contents", $_query, true)." LIMIT 50;");
	while ($dbCore->nextRecord())
	{
		$_kbarticleidlist[] = $dbCore->Record["kbarticleid"];
	}

	if (!count($_kbarticleidlist))
	{
		return $_queryresult;
	}

	// ======= BUILD THE CATEGORY LIST FOR THIS GROUP =======
	$_kbcategoryidlist = array();
	$dbCore->query("SELECT kbarticlelinks.kbarticleid, kbcategories.categorytype FROM `". TABLE_PREFIX ."kbarticlelinks` AS kbarticlelinks LEFT JOIN `". TABLE_PREFIX ."kbcategories` AS kbcategories ON (kbarticlelinks.kbcategoryid = kbcategories.kbcategoryid) LEFT JOIN `". TABLE_PREFIX ."tgroupassigns` AS tgroupassigns ON (kbarticlelinks.kbcategoryid = tgroupassigns.toassignid AND tgroupassigns.type = '". TGROUP_KNOWLEDGEBASE ."') WHERE ((tgroupassigns.tgroupid = '". intval($_SWIFT["tgroup"]["tgroupid"]) ."') OR (kbarticlelinks.kbcategoryid = '0')) AND (kbarticlelinks.kbarticleid IN (". buildIN($_kbarticleidlist) .")) LIMIT 50;");
	while ($dbCore->nextRecord())
	{
		if ($dbCore->Record["categorytype"] == SWIFTPUBLIC || empty($dbCore->Record["categorytype"]))
		{
			$_finalkbarticleidlist[] = $dbCore->Record["kbarticleid"];
		}
	}

	if (!count($_finalkbarticleidlist))
	{
		return $_queryresult;
	}

	// Now fetch the actual articles
	$_index = 1;
	$_queryresult[0]["querytype"] = "knowledgebase";
	$_queryresult[0]["action"] = "title";

	$dbCore->query("SELECT kbarticles.*, kbarticledata.* FROM `". TABLE_PREFIX ."kbarticles` AS kbarticles LEFT JOIN `". TABLE_PREFIX ."kbarticledata` AS kbarticledata ON (kbarticles.kbarticleid = kbarticledata.kbarticleid) WHERE kbarticles.kbarticleid IN (". buildIN($_finalkbarticleidlist) .");", 3);
	while ($dbCore->nextRecord(3))
	{
		if ($dbCore->Record3["articlestatus"] == "published")
		{
			$_queryresult[$_index] = $dbCore->Record3;
			$_queryresult[$_index]["querytype"] = "knowledgebase";
			$_queryresult[$_index]["searchcontents"] = substr(strip_tags($dbCore->Record3["contents"]),0,255);
			$_index++;
		}
	}

	return $_queryresult;
}

/**
* Searches the downloads
*/
function searchDownloads($_query)
{
	global $dbCore, $_SWIFT;

	$_itemidlist = $_finalitemidlist = $_queryresult = array();
	$dbCore->query("SELECT `downloaditemid` FROM `". TABLE_PREFIX ."downloaditems` WHERE (". buildSQLSearch("title", $_query).") OR (". buildSQLSearch("filename", $_query).") LIMIT 50;");
	while ($dbCore->nextRecord())
	{
		$_itemidlist[] = $dbCore->Record["downloaditemid"];
	}

	$dbCore->query("SELECT `downloaditemid` FROM `". TABLE_PREFIX ."downloaddesc` WHERE ". buildSQLSearch("description", $_query)." LIMIT 50;");
	while ($dbCore->nextRecord())
	{
		$_itemidlist[] = $dbCore->Record["downloaditemid"];
	}

	if (!count($_itemidlist))
	{
		return $_queryresult;
	}

	// ======= BUILD THE CATEGORY LIST FOR THIS GROUP =======
	$_categoryidlist = array();
	$dbCore->query("SELECT downloaditems.downloaditemid, downloadcategories.categorytype FROM `". TABLE_PREFIX ."downloaditems` AS downloaditems LEFT JOIN `". TABLE_PREFIX ."downloadcategories` AS downloadcategories ON (downloaditems.downloadcategoryid = downloadcategories.downloadcategoryid) LEFT JOIN `". TABLE_PREFIX ."tgroupassigns` AS tgroupassigns ON (downloadcategories.downloadcategoryid = tgroupassigns.toassignid AND tgroupassigns.type = '". TGROUP_DOWNLOADS ."') WHERE ((tgroupassigns.tgroupid = '". intval($_SWIFT["tgroup"]["tgroupid"]) ."') OR (downloaditems.downloadcategoryid = '0')) AND (downloaditems.downloaditemid IN (". buildIN($_itemidlist) .")) LIMIT 50;");
	while ($dbCore->nextRecord())
	{
		if ($dbCore->Record["categorytype"] == SWIFTPUBLIC || empty($dbCore->Record["categorytype"]))
		{
			$_finalitemidlist[] = $dbCore->Record["downloaditemid"];
		}
	}

	if (!count($_finalitemidlist))
	{
		return $_queryresult;
	}

	// Fetch the download items
	$_index = 1;
	$_queryresult[0]["querytype"] = "downloads";
	$_queryresult[0]["action"] = "title";

	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."downloaditems` AS downloaditems LEFT JOIN `". TABLE_PREFIX ."downloaddesc` AS downloaddesc ON (downloaditems.downloaditemid = downloaddesc.downloaditemid) WHERE downloaditems.downloaditemid IN (". buildIN($_finalitemidlist) .");");
	while ($dbCore->nextRecord())
	{
		$_queryresult[$_index] = $dbCore->Record;
		$_queryresult[$_index]["querytype"] = "downloads";
		$_queryresult[$_index]["searchcontents"] = substr(strip_tags($dbCore->Record["description"]),0,255);
		$_index++;
	}

	return $_queryresult;
}

/**
* Searches the troubleshooter
*/
function searchTroubleshooter($_query)
{
	global $dbCore, $_SWIFT;

	$_itemidlist = $_finalitemidlist = $_queryresult = array();
	$dbCore->query("SELECT `troubleshooterid` FROM `". TABLE_PREFIX ."troubleshootersteps` WHERE ". buildSQLSearch("subject", $_query)." LIMIT 50;");
	while ($dbCore->nextRecord())
	{
		$_itemidlist[] = $dbCore->Record["downloaditemid"];
	}

	$dbCore->query("SELECT `troubleshooterid` FROM `". TABLE_PREFIX ."troubleshooterdata` WHERE ". buildSQLSearch("contents", $_query).";");
	while ($dbCore->nextRecord())
	{
		$_itemidlist[] = $dbCore->Record["troubleshooterid"];
	}

	if (!count($_itemidlist))
	{
		return $_queryresult;
	}

	// ======= BUILD THE CATEGORY LIST FOR THIS GROUP =======
	$_categoryidlist = array();
	$dbCore->query("SELECT troubleshootersteps.troubleshooterid FROM `". TABLE_PREFIX ."troubleshootersteps` AS troubleshootersteps LEFT JOIN `". TABLE_PREFIX ."troubleshootercat` AS troubleshootercat ON (troubleshootersteps.troubleshootercatid = troubleshootercat.troubleshootercatid) LEFT JOIN `". TABLE_PREFIX ."tgroupassigns` AS tgroupassigns ON (troubleshootercat.troubleshootercatid = tgroupassigns.toassignid AND tgroupassigns.type = '". TGROUP_TROUBLESHOOTER ."') WHERE (tgroupassigns.tgroupid = '". intval($_SWIFT["tgroup"]["tgroupid"]) ."') AND (troubleshootersteps.troubleshooterid IN (". buildIN($_itemidlist) .")) LIMIT 50;");
	while ($dbCore->nextRecord())
	{
		$_finalitemidlist[] = $dbCore->Record["troubleshooterid"];
	}

	if (!count($_finalitemidlist))
	{
		return $_queryresult;
	}

	// Fetch the troubleshooter steps
	$_index = 1;
	$_queryresult[0]["querytype"] = "troubleshooter";
	$_queryresult[0]["action"] = "title";

	$dbCore->query("SELECT * FROM `". TABLE_PREFIX ."troubleshootersteps` AS troubleshootersteps LEFT JOIN `". TABLE_PREFIX ."troubleshooterdata` AS troubleshooterdata ON (troubleshootersteps.troubleshooterid = troubleshooterdata.troubleshooterid) WHERE troubleshootersteps.troubleshooterid IN (". buildIN($_finalitemidlist) .");");
	while ($dbCore->nextRecord())
	{
		$_queryresult[$_index] = $dbCore->Record;
		$_queryresult[$_index]["querytype"] = "troubleshooter";
		$_queryresult[$_index]["searchcontents"] = substr(strip_tags($dbCore->Record["contents"]),0,255);
		$_index++;
	}

	return $_queryresult;
}

/**
* Merges the Search Array
*/
function search_merge($_mergewhat, $_mergewith)
{
	$_index = count($_mergewhat);

	if (!_is_array($_mergewith))
	{
		return $_mergewhat;
	}

	foreach ($_mergewith as $key=>$val)
	{
		$_mergewhat[$_index] = $val;
		$_index++;
	}

	return $_mergewhat;
}
?>